// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.metadata;

import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.metadata.MetadataColumn;
import org.dengues.core.metadata.MetadataTable;
import org.dengues.core.metadata.types.DenguesTypesManager;
import org.dengues.core.metadata.types.JavaType;
import org.dengues.core.metadata.types.MetadataTypesManager;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 */
public class MetadataTableViewerProvider extends LabelProvider implements IStructuredContentProvider, ITableLabelProvider,
        ICellModifier, ITableColorProvider {

    private final MetadataTableViewer metadataTableViewer;

    /**
     * Qiang.Zhang.Adolf@gmail.com MetadataTableViewerProvider constructor comment.
     * 
     * @param metadataTableViewer
     */
    public MetadataTableViewerProvider(MetadataTableViewer metadataTableViewer) {
        this.metadataTableViewer = metadataTableViewer;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getImage(java.lang.Object)
     */
    @Override
    public Image getImage(Object element) {
        return super.getImage(element);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(Object element) {
        return super.getText(element);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object inputElement) {
        if (inputElement instanceof MetadataTable) {
            List<MetadataColumn> columns = ((MetadataTable) inputElement).getColumns();
            if (columns != null) {
                return columns.toArray(new MetadataColumn[0]);
            }
        }
        return new Object[0];
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object,
     * java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
     */
    public Image getColumnImage(Object element, int columnIndex) {
        MetadataColumn column = (MetadataColumn) element;
        if (columnIndex == 0) {
            if (column.isKey()) {
                return ImageUtil.getImage(IDenguesSharedImage.TABLEVIEW_KEY);
            } else {
                return ImageUtil.getImage(IDenguesSharedImage.TABLEVIEW_NULLKEY);
            }
        } else if (columnIndex == 1) {
            if (column.isKey()) {
                return ImageUtil.getImage(IDenguesSharedImage.EDITOR_CHECKBOXON);
            } else {
                return ImageUtil.getImage(IDenguesSharedImage.EDITOR_CHECKBOXOFF);
            }
        } else if (columnIndex == 5) {
            if (column.isNullable()) {
                return ImageUtil.getImage(IDenguesSharedImage.EDITOR_CHECKBOXON);
            } else {
                return ImageUtil.getImage(IDenguesSharedImage.EDITOR_CHECKBOXOFF);
            }
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
     */
    public String getColumnText(Object element, int columnIndex) {
        MetadataColumn column = (MetadataColumn) element;
        switch (columnIndex) {
        case 1:
            // return DenguesTextUtil.getNotNullString(Boolean.valueOf(column.isKey()).toString());
            return null;
        case 2:
            return DenguesTextUtil.getNotNullString(column.getColumnName());
        case 3:
            return DenguesTextUtil.getNotNullString(column.getType());
        case 4:
            return DenguesTextUtil.getNotNullString(column.getPattern());
        case 5:
            return null;
            // return DenguesTextUtil.getNotNullString(Boolean.valueOf(column.isNullable()).toString());
        case 6:
            return DenguesTextUtil.getNotNullString(column.getLength().toString());
        case 7:
            return DenguesTextUtil.getNotNullString(column.getPrecision().toString());
        case 8:
            return DenguesTextUtil.getNotNullString(column.getDefaut());
        case 9:
            return DenguesTextUtil.getNotNullString(column.getComment());
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
     */
    public boolean canModify(Object element, String property) {
        if (MetadataTableViewer.PROP_NAME.equals(property)) {
            return metadataTableViewer.isCanModifyName();
        } else if (MetadataTableViewer.SPACES.equals(property)) {
            return false;
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
     */
    public Object getValue(Object element, String property) {
        MetadataColumn column = (MetadataColumn) element;
        if (property.equals(MetadataTableViewer.PROP_COMMENT)) {
            return DenguesTextUtil.getNotNullString(column.getComment());
        } else if (property.equals(MetadataTableViewer.PROP_DEFAULT)) {
            return DenguesTextUtil.getNotNullString(column.getDefaut());
        } else if (property.equals(MetadataTableViewer.PROP_KEY)) {
            // return
            // Arrays.asList(MetadataTableViewer.BOOLEAN_ITEMS).indexOf(Boolean.valueOf(column.isKey()).toString());
            return column.isKey();
        } else if (property.equals(MetadataTableViewer.PROP_LENGTH)) {
            return DenguesTextUtil.getNotNullString(column.getLength().toString());
        } else if (property.equals(MetadataTableViewer.PROP_NAME)) {
            return DenguesTextUtil.getNotNullString(column.getColumnName());
        } else if (property.equals(MetadataTableViewer.PROP_NULLABLE)) {
            return column.isNullable();
            // return
            // Arrays.asList(MetadataTableViewer.BOOLEAN_ITEMS).indexOf(Boolean.valueOf(column.isNullable()).toString());
        } else if (property.equals(MetadataTableViewer.PROP_PATTERN)) {
            return DenguesTextUtil.getNotNullString(column.getPattern());
        } else if (property.equals(MetadataTableViewer.PROP_PRECISION)) {
            return DenguesTextUtil.getNotNullString(column.getPrecision().toString());
        } else if (property.equals(MetadataTableViewer.PROP_DBTYPE)) {
            return MetadataTypesManager.getInstance().indexOfDBType(column.getType());
        }
        return ""; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
     */
    public void modify(Object element, String property, Object value) {
        TableItem tableItem = ((TableItem) element);
        MetadataColumn column = (MetadataColumn) tableItem.getData();
        if (property.equals(MetadataTableViewer.PROP_COMMENT)) {
            column.setComment(DenguesTextUtil.getNotNullString((String) value));
        } else if (property.equals(MetadataTableViewer.PROP_DEFAULT)) {
            column.setDefaut(DenguesTextUtil.getNotNullString((String) value));
        } else if (property.equals(MetadataTableViewer.PROP_KEY)) {
            // String string = MetadataTableViewer.BOOLEAN_ITEMS[(Integer) value];
            // column.setKey(string.equals(Boolean.TRUE.toString()));
            column.setKey((Boolean) value);
        } else if (property.equals(MetadataTableViewer.PROP_LENGTH)) {
            column.setLength(Integer.parseInt(DenguesTextUtil.getNotNullString((String) value)));
        } else if (property.equals(MetadataTableViewer.PROP_NAME)) {
            column.setColumnName(DenguesTextUtil.getNotNullString((String) value));
        } else if (property.equals(MetadataTableViewer.PROP_NULLABLE)) {
            // String string = MetadataTableViewer.BOOLEAN_ITEMS[(Integer) value];
            // column.setNullable(string.equals(Boolean.TRUE.toString()));
            column.setNullable((Boolean) value);
        } else if (property.equals(MetadataTableViewer.PROP_PATTERN)) {
            JavaType javaType = MetadataTypesManager.getJavaTypeFromDBType(column.getType());
            if (javaType == DenguesTypesManager.DATE) {
                column.setPattern(DenguesTextUtil.getNotNullString((String) value));
            }
        } else if (property.equals(MetadataTableViewer.PROP_PRECISION)) {
            column.setPrecision(Integer.parseInt(DenguesTextUtil.getNotNullString((String) value)));
        } else if (property.equals(MetadataTableViewer.PROP_DBTYPE)) {
            String dbType = MetadataTypesManager.getInstance().getDBTypesLabels()[(Integer) value];
            column.setType(dbType);
            column.setPrecision(MetadataTypesManager.getDefaultPrecision(dbType));
            column.setLength(MetadataTypesManager.getDefaultLength(dbType));
        }
        metadataTableViewer.getTableViewer().refresh();
        // TableColumn[] columns = metadataTableViewer.getTableViewer().getTable().getColumns();
        // for (TableColumn tableColumn : columns) {
        // tableColumn.pack();
        // }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITableColorProvider#getBackground(java.lang.Object, int)
     */
    public Color getBackground(Object element, int columnIndex) {
        if (columnIndex == 2 && !metadataTableViewer.isCanModifyName()) {
            return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
        }
        if (columnIndex == 4) {
            return Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITableColorProvider#getForeground(java.lang.Object, int)
     */
    public Color getForeground(Object element, int columnIndex) {
        return null;
    }

}
